<meta charset="UTF-8">
<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 session_start();
 if(@$_SESSION['username']==""){
    echo "<script>alert('请先登录，后购物!');history.back();</script>";
	exit;
  }
?>
<?php
 /**
  * @todo 添加分页功能
  */
 include("top.php");
?>
<table width="766" height="438" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="229" height="438" valign="top" bgcolor="#F4F4F4"><?php include("left.php");?></td>
    <td width="561" align="center" valign="top" bgcolor="#FFFFFF"><table width="550" height="10" border="0" align="center" cellpadding="0" cellspacing="0">
    </table>
      <table width="557" border="0" align="center" cellpadding="0" cellspacing="0">
        <form name="form1" method="post" action="cart.php">
          <tr>
            <td height="46"></td>
          </tr>
          <tr>
            <td  bgcolor="#FFFFFF"><table width="500" border="0" align="center" cellpadding="0" cellspacing="1">
                <?php
			  //session_register("total");
                          $_SESSION['total'] = "";
                          
			  if(@$_GET['qingkong']=="yes"){
			     $_SESSION['booklist']="";
				 $_SESSION['quantity']=""; ///< @remark quantity拼成了quatity,这英文水平,啧啧
			  }
			   @$arraygwc=explode("@",$_SESSION['booklist']);
			   $s=0;
			   for($i=0;$i<count($arraygwc);$i++){
			       $s+=intval($arraygwc[$i]);
			   }
			  if($s==0 ){
				   echo "<tr>";
                   echo" <td height='25' colspan='6' bgcolor='#FFFFFF' align='center'>您的购物车为空!</td>";
                   echo"</tr>";
				}
			  else{ 
			?>
                <tr>
                  <td width="100" height="25" bgcolor="#FFFFFF"><div align="center"></div></td>
                  <td width="125" height="25" bgcolor="#B0E0E6"><div align="center">图书名称</div></td>
                  <td width="52" bgcolor="#B0E0E6"><div align="center">数量</div></td>
                  <td width="64" bgcolor="#B0E0E6"><div align="center">单价</div></td>
                  <td width="66" bgcolor="#B0E0E6"><div align="center">小计</div></td>
                  <td width="71" bgcolor="#B0E0E6"><div align="center">操作</div></td>
                </tr>
                <?php
			    $total=0;
			    $array=explode("@",$_SESSION['booklist']);
				$arrayquatity=explode("@",$_SESSION['quantity']);
				 while(list($name,$value)=each($_POST)){
					  for($i=0;$i<count($array)-1;$i++){
					    if(($array[$i])==$name){
						  $arrayquatity[$i]=$value;  
						}
					}
				}
			    $_SESSION['quantity']=implode("@",$arrayquatity);
				
				for($i=0;$i<count($array)-1;$i++){ 
				   $book_id=$array[$i];
				   $num=$arrayquatity[$i];
				  
				  if($book_id!=""){
				   $sql= exectue_sql_statement($conn,"select * from Book where book_id=$book_id");
				   $info=sqlsrv_fetch_array($sql);
				   $total1=$num*$info['unit_price'];
				   $total+=$total1;
				   $_SESSION["total"]=$total;
			?>
                <tr>
                    <td height="25" bgcolor="#FFFFFF"><div align="center"><image src="<?php echo $info['book_img'];?>" width="50" height="50" border="0"></div></td>
                  <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $info['book_name'];?></div></td>
                  <td height="25" bgcolor="#FFFFFF"><div align="center">
                  <input type="text" name="<?php echo $info['book_id'];?>" size="2" value=<?php echo $num;?>>
                  </div></td>
                  <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $info['unit_price'];?>元</div></td>
                  <td height="25" bgcolor="#FFFFFF"><div align="center"><?php echo $num*$info['unit_price']."元";?></div></td>
                  <td height="25" bgcolor="#FFFFFF"><div align="center"><a href="removecart.php?book_id=<?php echo $info['book_id']?>">移除</a></div></td>
                </tr>
                <?php
			      }
				 }
			 ?>
                <tr>
                  <td height="25" colspan="8" bgcolor="#FFFFFF"><div align="right">
                      <table width="500" height="25" border="0" align="center" cellpadding="0" cellspacing="0">
                        <tr>
                          <td width="125"><div align="center">
                              <input name="submit2" type="submit" value="更改商品数量">
                          </div></td>
                          <td width="125"><div align="center"><a href="finishcart.php">去收银台</a></div></td>
                          <td width="125"><div align="center"><a href="cart.php?qingkong=yes">清空购物车</a></div></td>
                          <td width="125"><div align="left">总计：<?php echo $total."元";?></div></td>
                        </tr>
                      </table>
                  </div></td>
                </tr>
                <?php
			 }
			?>
            </table></td>
          </tr>
        </form>
    </table></td>
  </tr>
</table>
<?php
 include("bottom.php");
?>